package com.ctshk.rpc.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.payment.CallPayDTO;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.utils.Assert;
import com.ctshk.rpc.payment.dto.PayDetailsDTO;
import com.ctshk.rpc.payment.entity.PayRecord;
import com.ctshk.rpc.payment.enums.PayOrderStatus;
import com.ctshk.rpc.payment.enums.PaymentMethodClass;
import com.ctshk.rpc.payment.mapper.PayRecordMapper;
import com.ctshk.rpc.payment.req.PayCommonReq;
import com.ctshk.rpc.payment.service.IPayRecordService;
import com.ctshk.rpc.payment.strategy.PayContext;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * <p>
 * 支付记录 服务实现类
 * </p>
 *
 * @author hef
 * @since 2021-04-21
 */
@DubboService
public class PayRecordServiceImpl extends ServiceImpl<PayRecordMapper, PayRecord> implements IPayRecordService {
    @Autowired
    private PayRecordMapper recordMapper;

    @Override
    public Integer savePayRecord(PayCommonReq req, CallPayDTO callPayDTO, int businessTypeCode) {
        recordMapper.update(null, new UpdateWrapper<PayRecord>().lambda()
                .eq(PayRecord::getOrderId, req.getOrderId())
                .set(PayRecord::getIsDeleted, IsDeletedCode.YES.getCode()));
        //支付记录
        PayRecord payRecord = new PayRecord();
        payRecord.setId(IdWorker.getId());
        payRecord.setOrderId(req.getOrderId());
        payRecord.setOrderNumber(callPayDTO.getOrderNumber());
        payRecord.setBusinessTypeCode(businessTypeCode);
        payRecord.setPaymentMethod(req.getPaymentMethod().getCode());
        payRecord.setPaymentMethodName(req.getPaymentMethod().getName());
        payRecord.setCurrency(req.getCurrency().getCode());
        payRecord.setTotalFee(req.getTotalFee());
        payRecord.setSubject(req.getSubject());
        payRecord.setOrderStatus(PayOrderStatus.UNPAID.getStatus());
        payRecord.setPayReqParam(JSON.toJSONString(req));
        payRecord.setCreateId(callPayDTO.getSalesmanId());
        payRecord.setModifiedId(callPayDTO.getSalesmanId());
        payRecord.setNotifyUrl(req.getNotifyUrl());
        payRecord.setIsDeleted(IsDeletedCode.NO.getCode());
        payRecord.setGmtCreate(LocalDateTime.now());
        payRecord.setGmtModified(LocalDateTime.now());
        return recordMapper.insert(payRecord);
    }

    @Override
    public PayDetailsDTO queryTransactionInfo(String orderNumber) {
        PayRecord record = recordMapper.selectByOrderId(Long.valueOf(orderNumber));
        Assert.nonNull(record, SystemError.PAY_22004);
        //不是未支付的状态直接返回本地数据
        if (!PayOrderStatus.UNPAID.getStatus().equals(record.getOrderStatus())) {
            PayDetailsDTO payDetailsDTO = new PayDetailsDTO();
            payDetailsDTO.setOrderNumber(record.getOrderNumber());
            payDetailsDTO.setOrderTime(record.getGmtCreate());
            payDetailsDTO.setPaymentStatus(PayOrderStatus.getByStatus(record.getOrderStatus()));
            payDetailsDTO.setOrderAmount(record.getTotalFee());
            payDetailsDTO.setPaymentTime(record.getPayTime());
            return payDetailsDTO;
        }
        //查实时数据
        PayDetailsDTO dto = PayContext.queryTransaction(orderNumber,
                PaymentMethodClass.getClassByCode(record.getPaymentMethod()));
        //避免真实状态和本地记录状态不一致
        if (!dto.getPaymentStatus().getStatus().equals(record.getOrderStatus())) {
            PayRecord updateRecord = new PayRecord();
            updateRecord.setId(record.getId());
            updateRecord.setOrderStatus(dto.getPaymentStatus().getStatus());
            updateRecord.setTotalFee(dto.getOrderAmount());
            recordMapper.updateById(updateRecord);
        }
        return dto;
    }

}
